<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tools extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->library('parser');
    }

    public function index() {
        $url = 'http://www.truyencuoihay.vn/truyen-tieu-lam?pagenumber=';
        $this->load->helper('html_dom');
        $page = 1;
        while ($page > 0) {
            $urlload = $url . $page;

            $html = file_get_html($urlload);
            $arrData = array();
            
            foreach($html->find('.product-title a') as $key => $element) {
                $arrData[$key]['title'] = trim(strip_tags($element->innertext));
                $arrData[$key]['character'] = '';
                $arrData[$key]['check'] = 0;
            }

            foreach($html->find('.description') as $key => $element) {
                $arrData[$key]['content'] = strip_tags($this->br2nl($element->innertext));
            }

            /* foreach ($arrData as $key => $value) {
                $this->db->insert('t_vova', $value);
            } */
            //$arrData = $this->convertArrayKeysToUtf8($arrData);
            //var_dump($arrData);
            $this->db->insert_batch('t_vova', $arrData);
            $page--;
        }
        $data['url_data'] = $url;
        $this->parser->parse('tools.php', $data, FALSE);
    }
    
    function convertArrayKeysToUtf8($s) {
        return utf8_decode($s);
    }

    /**
     * Convert BR tags to nl
     *
     * @param string The string to convert
     * @return string The converted string
     */
    function br2nl($string)
    {
        return trim(preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string));
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */